/**
 * 角色管理API服务
 * @description 提供角色管理的所有API接口
 */

import requestUtil from '@/utils/request'

// 角色接口类型定义
export interface Role {
  id: number
  name: string
  key: string
  status: number
  description: string
  created_at: string
  updated_at: string
}

export interface RoleForm {
  id?: number
  name: string
  key: string
  status?: number
  description: string
  sort?: number
  permissions?: number[]
}

export interface RoleListParams {
  page?: number
  page_size?: number
  name?: string
  key?: string
  status?: string
}

export interface RoleListResponse {
  code: number
  message: string
  data: {
    results: Role[]
    total: number
    page: number
    page_size: number
    total_pages: number
  }
}

export interface RoleResponse {
  code: number
  message: string
  data: Role
}

/**
 * 获取角色列表
 * @param params 查询参数
 * @returns Promise<RoleListResponse>
 */
export function getRoleList(params: RoleListParams = {}): Promise<RoleListResponse> {
  return requestUtil.get('/api/role/list/', params).then(response => response.data)
}

/**
 * 获取角色详情
 * @param roleId 角色ID
 * @returns Promise<RoleResponse>
 */
export function getRoleDetail(roleId: number): Promise<RoleResponse> {
  return requestUtil.get(`/api/role/${roleId}/`).then(response => response.data)
}

/**
 * 创建角色
 * @param roleData 角色数据
 * @returns Promise<RoleResponse>
 */
export function createRole(roleData: RoleForm): Promise<RoleResponse> {
  return requestUtil.post('/api/role/list/', roleData).then(response => response.data)
}

/**
 * 更新角色
 * @param roleId 角色ID
 * @param roleData 角色数据
 * @returns Promise<RoleResponse>
 */
export function updateRole(roleId: number, roleData: RoleForm): Promise<RoleResponse> {
  return requestUtil.put(`/api/role/${roleId}/`, roleData).then(response => response.data)
}

/**
 * 删除角色
 * @param roleId 角色ID
 * @returns Promise<{code: number, message: string}>
 */
export function deleteRole(roleId: number): Promise<{code: number, message: string}> {
  return requestUtil.del(`/api/role/${roleId}/`).then(response => response.data)
}
